from django.db import models
from common.db import BaseModel


class Category(models.Model):
    name = models.CharField(max_length=50, unique=True, verbose_name='分类名称')
    level = models.IntegerField(default=1, verbose_name='分类等级')
    pid = models.ForeignKey('self', related_name='children', null=True, blank=True, on_delete=models.CASCADE,
                            verbose_name='父级分类Id')

    class Meta:
        db_table = 'category'
        verbose_name = '分类'
        verbose_name_plural = verbose_name

    def attrs_static_list(self):
        return [{'id': attr.id, 'name': attr.name, 'val': attr.val, 'type': attr._type, 'cid': self.id} for attr in
                self.attrs.all().filter(_type=0)]

    def attrs_dynamic_list(self):
        return [{'id': attr.id, 'name': attr.name, 'val': attr.val, 'type': attr._type, 'cid': self.id} for attr in
                self.attrs.all().filter(_type=1)]


class Attr(models.Model):
    type_choices = (
        (0, 'static'),
        (1, 'dynamic')
    )
    name = models.CharField(max_length=50, unique=True, verbose_name='属性名称')
    val = models.CharField(max_length=255, unique=True, verbose_name='属性值')
    _type = models.CharField(choices=type_choices, default=0, max_length=10, verbose_name='属性类型')
    cid = models.ForeignKey(Category, related_name='attrs', on_delete=models.CASCADE, verbose_name='分类id')

    class Meta:
        db_table = 'attr'
        verbose_name = '属性'
        verbose_name_plural = verbose_name


class Goods(models.Model):
    name = models.CharField(max_length=50, unique=True, verbose_name='商品名称')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品价格')
    number = models.IntegerField(verbose_name='商品数量')
    introduce = models.TextField(verbose_name='商品介绍')
    big_log = models.CharField(max_length=255, verbose_name='商品大图', null=True, blank=True)
    small_log = models.CharField(max_length=255, verbose_name='商品大图', null=True, blank=True)
    state = models.IntegerField(default=0, verbose_name='商品状态')  # 0未通过 1审核中 2已审核
    is_promote = models.IntegerField(default=0, verbose_name='是否促销')  # 0否 1是
    hot_number = models.IntegerField(default=0, verbose_name='热销数量')
    weight = models.IntegerField(default=0, verbose_name='商品重量')

    cid_one = models.ForeignKey(Category, related_name='good_one', on_delete=models.CASCADE, verbose_name='一级分类id')
    cid_two = models.ForeignKey(Category, related_name='goods_two', on_delete=models.CASCADE, verbose_name='二级分类id')
    cid_three = models.ForeignKey(Category, related_name='good_three', on_delete=models.CASCADE,
                                  verbose_name='三级分类id')

    atts = models.ManyToManyField(Attr, through='GoodsAttr', through_fields=('gid', 'aid'), verbose_name='属性')

    class Meta:
        db_table = 'goods'
        verbose_name = '商品'
        verbose_name_plural = verbose_name

    def image_url(self):
        return self.big_log

    def atts_list(self):
        return [{'id': att.id, 'name': att.name, 'val': att.val, 'type': att._type} for att in self.atts.all()]

    def category_one(self):
        return {'id': self.cid_one.id, 'name': self.cid_one.name}

    def category_two(self):
        return {'id': self.cid_two.id, 'name': self.cid_two.name}

    def category_three(self):
        return {'id': self.cid_three.id, 'name': self.cid_three.name}


class GoodsAttr(models.Model):
    type_choices = (
        (0, 'static'),
        (1, 'dynamic')
    )
    gid = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='商品id')
    aid = models.ForeignKey(Attr, on_delete=models.CASCADE, verbose_name='属性id')
    val = models.CharField(max_length=255, verbose_name='属性值')
    _type = models.CharField(choices=type_choices, default=0, max_length=10, verbose_name='属性类型')

    class Meta:
        db_table = 'goods_attrs'
        verbose_name = '商品属性'
        verbose_name_plural = verbose_name
